<?php 
//ProfessorSearch.php
//Implemented 2/10/10 
//@author Michael Slaughter

//updated
//@author William Harris


require_once("overhead.php");
require_once("Search.php");

class ProfessorSearch{
	var $name; 
	var $professors;

// Setters required based on input
	public function setName($inNames){
		$this->name = $inNames;
	}	
	public function clear(){//Sets all objects to nulls
		$this->name = null;
		$this->professors = null;
	}
	public function query(){ //Sets the Section array of this instance to the results of this query 

//SELECT * FROM Professor where Firstname like "%b%" or Lastname like "%s%"
		$queryString = "select Professor.* from Professor"; 
		$this->professors = array();
		//Split up String depending on input
		
		//Looking for a String that is "Lastname, Firstname"
		if strpos($name,",") !== false{
			$spaceName = explode(" ",$name);
			$firstName = $spaceName[1];
			$lastName = $spaceName[0];
			$queryString = "select Professor.* from Professor where FirstName like '%".mysql_real_escape_string($firstName)."%' or LastName like '%"mysql_real_escape_string($lastName)"%'";					
		}
		//Looking for a string that is "Firstname Lastname"
		else if strpos($name," ") !== false{
			$spaceName = explode(" ",$name);
			$firstName = $spaceName[0];
			$lastName = $spaceName[1];
			$queryString = "select Professor.* from Professor where FirstName like '%".mysql_real_escape_string($firstName)."%' or LastName like '%"mysql_real_escape_string($lastName)"%'";			
		} 

		else (isset($this->name)){
			$queryString = "select Professor.* from Professor where FirstName like '%".mysql_real_escape_string($this->name)."%' or LastName like '%".$this->mysql_real_escape_string(name)."%'";			
		}
		
	//var_dump($queryString);
	
		$result = mysql_query($queryString);
		
		$this->professors = array();
		
		if($result){
		
			$i = 0;
			
				while($row = mysql_fetch_assoc($result)){
					$prof = new Professor();
					$prof->retrieveFromID($row['ID']); //corresponds to the Primary key in the Section table (also called ID)
					$prof->setLastName($row['LastName']); //coresponds to the Proffessor's last name
					$prof->setFirstName($row['FirstName']); //coresponds to the Proffessor's last name
					$this->professors[$i] = $prof;
					
					$i++;
				} 
			
			
		} else {
			
			return false;
		
		}
	}
	public function getAllResults(){
		return $this->professors; //Return the array of Professors gained off of a successful query
	}
	public function getResult($index){
		return $this->professors[$index]; //Return the Professor object at the position index
	}
	public function sort($fieldName){
	/* Will implement at a later date */ 
	}
		
}
